package com.library.repository.impl;/*
 *@author lzg
 *@data 2020/11/22
 * 将借书的数据存储到数据库中
 */

import com.library.entity.Book;
import com.library.entity.BorrowRecode;
import com.library.entity.Reader;
import com.library.repository.BorrowRepository;
import com.library.utils.JDBCTools;
import jdk.nashorn.internal.scripts.JD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BorrowRepositoryImpl implements BorrowRepository {
    @Override
    public void insertData(Integer bookId, Integer readerId, String borrowTime, String returnTime, Integer adminId) {
        Connection connection = JDBCTools.getConnection();
        String sql = "insert into borrow(bookid,readerid,borrowtime,returntime,adminid,state) value(?,?,?,?,?,0)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,bookId);
            preparedStatement.setObject(2,readerId);
            preparedStatement.setObject(3,borrowTime);
            preparedStatement.setObject(4,returnTime);
            preparedStatement.setObject(5,adminId);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection,preparedStatement);
        }
    }

    @Override
    public List<BorrowRecode> findAllBorrowRecodeByReaderId(Integer readerId) {
        //读取数据库的数据，按行封装到BorrowRecode中，在添加到List<>
        Connection connection = JDBCTools.getConnection();
        String sql = "select br.id, b.`name`,b.author,b.publish,r.`name`,r.tel,r.cardid,br.borrowtime,br.returntime,br.state FROM book b, reader r,borrow br\n" +
                "                WHERE b.id = br.bookid and br.readerid = r.id and r.id = ?;";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<BorrowRecode> borrowRecodesList = new ArrayList<BorrowRecode>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,readerId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                //添加对象到列表，不创建栈对象，节省内存空间
                borrowRecodesList.add(new BorrowRecode(resultSet.getInt(1),
                                                        new Book(resultSet.getString(2),resultSet.getString(3),resultSet.getString(4)),
                                                        new Reader(resultSet.getString(5),resultSet.getString(6),resultSet.getString(7)),
                                                        resultSet.getString(8),
                                                        resultSet.getString(9),
                                                        resultSet.getInt(10)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection,preparedStatement,resultSet);
        }
        return borrowRecodesList;
    }
}
